package com.cinyi.ormx.android.ui;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.cinyi.ormx.android.App;
import com.cinyi.ormx.android.R;
import com.cinyi.ormx.android.model.Tresource;
import com.cinyi.ormx.android.test.TresourceTest;

/**
 * @author penghui
 * 
 */
public class Main extends BaseAct implements OnClickListener{

	private static final String TAG = "MAIN" ;

	//
	public static TextView mTextView;

	private TresourceTest test ;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		App.getInstance().onServiceStarted();
		mTextView = (TextView)findViewById(R.id.t_get_info);
		findViewById(R.id.b_dao_delete).setOnClickListener(this);
		findViewById(R.id.b_dao_save_info).setOnClickListener(this);
		findViewById(R.id.b_model_delete).setOnClickListener(this);
		findViewById(R.id.b_model_save_info).setOnClickListener(this);
		findViewById(R.id.b_sql_save).setOnClickListener(this);
		findViewById(R.id.b_sql_select).setOnClickListener(this);
		
		findViewById(R.id.t_get_info).setOnClickListener(this);
		
		test = new TresourceTest();
		test.setup(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.b_dao_delete:{
			test.testDeleteResourceByDao();
			break ;
		}
		case R.id.b_dao_save_info:{
			test.testSaveResourceByDAO();
			break ;
		}
		case R.id.b_model_delete:{
			test.testDeleteResourceByModel();
			break ;
		}
		case R.id.b_model_save_info:{
			test.testSaveResourceByModel();
			break ;
		}
		case R.id.b_sql_save:{
			test.testSaveComplexSQL();
			break ;
		}
		case R.id.b_sql_select:{
			List<Tresource>  result = test.testQueryComplexModel();
			StringBuffer sb = new StringBuffer();
			for(Tresource r : result){
				sb.append(r.getId() + "-"+r.getName()).append("\r\n");
			}
			mTextView.setText(sb + "\r\n\t --- " + System.currentTimeMillis());
			
			break ;
		}
		case R.id.t_get_info:
			showQuery();
			break;
		}
	}

	private void showQuery() {
		Tresource t = new Tresource();
        t.setId("ddd3");
        t = t.find() ;
		String name = "unkown";
		if(t != null){
			name = t.getName() ;
		}
		mTextView.setText(name + " --- " + System.currentTimeMillis());
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
//		App.getInstance().onQuit();
	}

}
